﻿namespace SoftwareConsulting.BI11.NumericalMethods.Iterations
{
    /// <summary>
    /// Класс дроби незавершенной бета-функции
    /// </summary>
    public class IncompleteBetaFunctionFraction : ContinuedFraction
    {	    
        private double _alpha1;
        private double _alpha2;

        /// <summary>
        /// Конструктор класса
        /// </summary>
        /// <param name="alpha1"></param>
        /// <param name="alpha2"></param>
        public IncompleteBetaFunctionFraction(double alpha1, double alpha2)
        {
            _alpha1 = alpha1;
            _alpha2 = alpha2;
        }
        
        /// <summary>
        /// Вычисляет указанную дробь
        /// </summary>
        /// <param name="factorsIndex"></param>
        protected override void ComputeFactorsAt(int factorsIndex)
        {            
            int m = factorsIndex / 2;
            int m2 = 2 * m;
            Factors[0] = m2 == factorsIndex
                            ? X * m * (_alpha2 - m)
                                        / ((_alpha1 + m2) * (_alpha1 + m2 - 1))
                            : X * (_alpha1 + m) * (_alpha1 + _alpha2 + m)
                                        / ((_alpha1 + m2) * (_alpha1 + m2 + 1));
        }

        /// <summary>
        /// Инициализирует значение
        /// </summary>
        /// <returns></returns>
        protected override double InitializeValue()
        {
            Factors[1] = 1;
            return 1;
        }
    }
}
